package com.kull.hadoop;

import java.io.IOException;
import java.text.MessageFormat;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;

public class MaxTempMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
	private static final int MISSING = 9999;
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		super.map(key, value, context);
		String line = value.toString();
	    String year = line.substring(15, 19);
	    int airTemperature;
	    if (line.charAt(87) == '+') { // parseInt doesn't like leading plus signs
	      airTemperature = Integer.parseInt(line.substring(88, 92));
	    } else {
	      airTemperature = Integer.parseInt(line.substring(87, 92));
	    }
	    String quality = line.substring(92, 93);
	    if (airTemperature != MISSING && quality.matches("[01459]")) {
	      context.write(new Text(year), new IntWritable(airTemperature));
	      System.out.println(MessageFormat.format("{0} text:{1} iw:{2}",this.getClass().getName(),year,airTemperature ));
	    }
	}
	
}
